/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * semi_createUlbTrainingData.java
 *
 * Created on Mar 8, 2011, 4:14:31 PM
 */

package crfsvm.crf.een_phuong;

import java.awt.Color;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;
import javax.swing.text.JTextComponent;
import java.io.*; //my addition
import javax.swing.UIManager;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Thien
 */
public class semi_createUlbTrainingData extends javax.swing.JFrame {

    /** Creates new form semi_createUlbTrainingData */
    public semi_createUlbTrainingData() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(semi_createUlbTrainingData.class);
        jTextField1.setText(resourceMap.getString("inField.text")); // NOI18N
        jTextField1.setName("inField"); // NOI18N

        jButton1.setText(resourceMap.getString("inPutton.text")); // NOI18N
        jButton1.setName("inPutton"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inButton_Action(evt);
            }
        });

        jTextField2.setText(resourceMap.getString("outField.text")); // NOI18N
        jTextField2.setName("outField"); // NOI18N

        jButton2.setText(resourceMap.getString("outButton.text")); // NOI18N
        jButton2.setName("outButton"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outButton_Action(evt);
            }
        });

        jButton3.setText(resourceMap.getString("execute.text")); // NOI18N
        jButton3.setName("execute"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(44, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(260, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addGap(255, 255, 255))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(27, 27, 27)
                .addComponent(jButton3)
                .addContainerGap(192, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void inButton_Action(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inButton_Action
        // TODO add your handling code here:
        JFileChooser choose = new JFileChooser();
        ExampleFileFilter filter = new ExampleFileFilter("txt","txt File");
        choose.addChoosableFileFilter(filter);
        int f = choose.showOpenDialog(this);
        if (f == JFileChooser.APPROVE_OPTION) {
            File inFile = choose.getSelectedFile();
            jTextField1.setText(inFile.getPath());
        }
    }//GEN-LAST:event_inButton_Action

    private void outButton_Action(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outButton_Action
        // TODO add your handling code here:
        JFileChooser choose = new JFileChooser();
        //ExampleFileFilter filter = new ExampleFileFilter();
        //choose.addChoosableFileFilter(filter);
        choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //choose.setAcceptAllFileFilterUsed(false);
        int f = choose.showOpenDialog(this);
        if (f == JFileChooser.APPROVE_OPTION) {
            File outFile = choose.getSelectedFile();
            jTextField2.setText(outFile.getPath());
        }
    }//GEN-LAST:event_outButton_Action

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        BufferedWriter bw = null;
        BufferedReader br = null;
        try
        {
             String save = "", line = "",toAppend = "";
             char c = 'a';
             int len = 0;
             br = new BufferedReader(new InputStreamReader(new FileInputStream(jTextField1.getText()), "UTF-8"));
             while ((line = br.readLine()) != null)
             {
                 line = line.trim();
                 toAppend = "";
                 c = 'a';
                 len = line.length();
                 if (line.length() != 0)
                 {
                     while (c != ' ')
                     {
                         c = line.charAt(len - 1);
                         len--;
                     }
                     save += line.substring(0, len).trim() + "\n";
                 }
                else
                    save += "\n";
             }

             br.close();

             bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jTextField2.getText()), "UTF-8"));
             bw.write(save);
             bw.flush();
             bw.close();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new semi_createUlbTrainingData().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
